﻿namespace Avatar.Schedule
{
    using Avatar;
    using System;

    public abstract class Task : ITask
    {
        protected Task()
        {
        }

        protected abstract void OnStart();

        public abstract bool IsUnique { get; }

        public void Start()
        {
            if (Started != null)
            {
                Started(this, EventArgs.Empty);
            }

            try
            {
                OnStart();

                if (Completed != null)
                {
                    Completed(this, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                if (Failed != null)
                {
                    Failed(this, new FailedEventArgs(this, ex));
                }
            }
        }

        public int Delay
        {
            get { return 100; }
        }

        public event EventHandler Started;

        public event EventHandler Completed;

        public event EventHandler<FailedEventArgs> Failed;
    }
}
